package jdk;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.ProtectionDomain;

public class ClassLoaderVisitor {

    private static Method method = null;

    static {
        try {
            for (Method m : ClassLoader.class.getDeclaredMethods()) {
                if (m.getName().equals("defineClass") && m.getParameterCount() > 4) {
                    method = m;
                    method.setAccessible(true);
                    break;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Class<?> defineClass(ClassLoader loader, String name, byte[] b, int off, int len,
                                       ProtectionDomain protectionDomain) throws InvocationTargetException, IllegalAccessException {
        return (Class<?>) method.invoke(loader, name, b, off, len, protectionDomain);
    }
}
